<?php

	class ForumController extends Zend_Controller_Action {
		
		public function init() {
			
			$this->view->headLink()
					->appendStylesheet($this->_request->getBaseUrl().'/styles/commun.css')
					->appendStylesheet($this->_request->getBaseUrl().'/styles/template.css')
					->appendStylesheet($this->_request->getBaseUrl().'/styles/forum.css');
			
			$this->view->action = $this->_request->getActionName();
			$this->view->controller = $this->_request->getControllerName();
		}
		
		public function indexAction() {
			
			$this->view->headTitle('Hybrid Legacy - Forum');
			
			$this->view->headLink()
					->appendStylesheet($this->_request->getBaseUrl().'/styles/frontend/form.css');
					
			$this->view->jQuery()
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.form.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.validation.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.validation.rules_custom.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.validation.messages_fr.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/frontend/contact.js'));
			
			// get subjects
			$sM = new Model_Subject();
			$filter = array();
			$subjects = $sM->get($filter);
			
			$this->view->subjects = $subjects;
			
		}
		
		public function prcContactAction() {
			
			$datas = $this->_request->getParams();
			
			// check
			$errors = array();
			
			// firstname
			if(is_null($datas['firstname']) or empty($datas['firstname'])) {
				$errors[] = 'Prénom';
			}
			
			// lastname
			if(is_null($datas['lastname']) or empty($datas['lastname'])) {
				$errors[] = 'Nom';
			}
			
			// email
			if(!is_null($datas['email']) and !empty($datas['email'])) {
				
				// check if is email
				$pattern='#^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$#'; 
				if(!preg_match($pattern, $datas['email'])) {
					$errors[] = 'Adresse email';
				}
			}
			
			// subject
			if(is_null($datas['subject']) or empty($datas['subject']) or !is_numeric($datas['subject'])) {
				$errors[] = 'Sujet';
			}
			
			// object
			if(is_null($datas['object']) or empty($datas['object'])) {
				$errors[] = 'Objet';
			}
			
			// message
			if(is_null($datas['message']) or empty($datas['message'])) {
				$errors[] = 'Message';
			
			} else {
				if(strlen($datas['message']) < 30) {
					$errors[] = 'Message (Doit contenir au minimum 30 caractères)';
				}
			}
			
			// if errors
			if(!empty($errors)) {
				$message = 'Veuillez vérifier les champs suivant :';
				$message .= '<ul>';
				foreach($errors as $e) {
					$message .= '<li>'.$e.'</li>';
				}
				$message .= '</ul>';
				
				exit(Zend_Json::encode(array('result' => false, 'message' => $message)));
			}
			
			$eM = new Model_Email();
			
			$email = array('firstname' => $datas['firstname'],
							'lastname' => $datas['lastname'],
							'email' => $datas['email'],
							'subject' => $datas['subject'],
							'object' => $datas['object'],
							'message' => $datas['message'],
							'ip' => $_SERVER['REMOTE_ADDR']
							);
			$result = $eM->send($email);
			
			if($result) {
			
				exit(Zend_Json::encode(array('result' => true, 'message' => 'Message envoyé')));
			
			} else {
				exit(Zend_Json::encode(array('result' => false, 'message' => 'Erreur lors de l\'envoi du message')));
			}
			
		}
		
	}
	
?>
